package com.ruoyi.web.controller.workflow;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.workflow.domain.BpmnInfo;
import com.ruoyi.workflow.service.IBpmnInfoService;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import static com.ruoyi.common.constant.FileConstants.FILE_SUFFIX_NAME;

/**
 * 流程定义信息Controller
 *
 * @author shier
 * @date 2022-11-14
 */
@RestController
@RequestMapping("/workflow/bpmnInfo")
public class BpmnInfoController extends BaseController {

    private final IBpmnInfoService bpmnInfoService;
    public BpmnInfoController(IBpmnInfoService bpmnInfoService) {
        this.bpmnInfoService = bpmnInfoService;
    }

    /**
     * 查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BpmnInfo bpmnInfo) {
        startPage();
        List<BpmnInfo> list = bpmnInfoService.selectBpmnInfoList(bpmnInfo);
        return getDataTable(list);
    }

    /**
     * 查看流程图/流程文件
     *
     * @param id   bpmnInfo对象的 id
     * @param type 要查看的是什么类型: bpmn OR svg
     * @param resp 响应对象,用于把流响应回去
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:view')")
    @GetMapping("/resource/{id}")
    public void viewResource(@PathVariable Long id, String type, HttpServletResponse resp) throws IOException {
        InputStream resource = bpmnInfoService.getResourceByType(id, type);
        // 将资源输入流拷贝到响应输出流
        IOUtils.copy(resource, resp.getOutputStream());
    }

    /**
     * 删除流程定义
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:remove')")
    @Log(title = "删除流程定义", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        if (ids == null || ids.length == 0) {
            return error("请选择要删除的流程定义");
        }
        return toAjax(bpmnInfoService.deleteBpmnInfoByIds(ids));
    }

    /**
     * 部署流程定义
     */
    @PreAuthorize("@ss.hasPermi('business:bpmnInfo:deploy')")
    @Log(title = "部署流程定义", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public AjaxResult deploy(MultipartFile file, BpmnInfo bpmnInfo, String dictType) throws IOException {

        // 1: 获取文件名(包含后缀)
        String fileName = file.getOriginalFilename();
        bpmnInfo.setFileName(fileName);

        // 2: 得到需要部署文件的后缀
        String fileSuffix = FileUtils.getFileSuffix(fileName);

        // 3: 判断部署文件的后缀名是否在指定的集合中
        if (!FILE_SUFFIX_NAME.contains(fileSuffix)) {
            // 不再进行提示
            return AjaxResult.error(HttpStatus.BAD_REQUEST, "目前仅支持提交" + FILE_SUFFIX_NAME + "类型的流程文件");
        }

        // 进行部署
        bpmnInfoService.deploy(bpmnInfo, file.getInputStream(), dictType);
        return AjaxResult.success("部署成功");
    }
}
